package com.wn.class30.service.impl;

import com.wn.class30.entity.UserAddressExample;
import com.wn.class30.mapper.UserAddressDao;
import com.wn.class30.service.UserAddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

@Service
public class UserAddressServiceImpl implements UserAddressService {
    @Resource
    private UserAddressDao userAddressDao;

    @Override
    public int selectCount(HashMap map) {
        UserAddressExample ex = new UserAddressExample();
        String name = (String) map.get("name");
        String tel = (String) map.get("tel");
        String addr = (String) map.get("addr");

        UserAddressExample.Criteria criteria = ex.createCriteria();
        if(!"".equals(name) && name != null){
            criteria.andNameLike("%"+name+"%");
        }
        if(!"".equals(tel) && tel != null){
            criteria.andTelEqualTo(tel);
        }
        if(!"".equals(addr) && addr != null){
            criteria.andAddrLike("%"+addr+"%");
        }
        return (int) userAddressDao.countByExample(ex);
    }

    @Override
    public Object select(HashMap map) {
        UserAddressExample ex = new UserAddressExample();
        String name = (String) map.get("name");
        String tel = (String) map.get("tel");
        String addr = (String) map.get("addr");
        Integer page = 0;
        if(!"".equals((String) map.get("page"))&&(String) map.get("page")!=null){
            page = Integer.parseInt((String) map.get("page"));
        }
        Integer limit = 0;
        if(!"".equals((String) map.get("limit"))&&(String) map.get("limit")!=null){
            limit = Integer.parseInt((String) map.get("limit"));
        }
        ex.setOffset(((long)(page-1))*limit);
        ex.setLimit(limit);
        UserAddressExample.Criteria criteria = ex.createCriteria();
        if(!"".equals(name) && name != null){
            criteria.andNameLike("%"+name+"%");
        }
        if(!"".equals(tel) && tel != null){
            criteria.andTelEqualTo(tel);
        }
        if(!"".equals(addr) && addr != null){
            criteria.andAddrLike("%"+addr+"%");
        }
        return userAddressDao.selectByExample(ex);
    }
}
